package com.ideal.manage.esms.controller.monitor;

import com.alibaba.fastjson.JSONObject;
import com.ideal.manage.esms.bean.project.EquipmentManage;
import com.ideal.manage.esms.bean.project.ParameterInfo;
import com.ideal.manage.esms.service.diagnostic.DiagnoseResultInfoService;
import com.ideal.manage.esms.service.monitor.MonitorService;
import com.ideal.manage.esms.service.project.EquipmentManageService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author 83730
 * @date 2018/5/25
 */
@Controller
@RequestMapping("monitor")
public class MonitorForOneController {
    private Logger logger = LoggerFactory.getLogger(MonitorController.class);

    @Resource
    private MonitorService monitorService;
    @Resource
    private EquipmentManageService equipmentManageService;
    @Resource
    private DiagnoseResultInfoService diagnoseResultInfoService;

    /**
     * 系统监控内嵌页面 - 电池单体
     *
     * @param model
     * @param id
     */
    @RequestMapping("monitor_web_for_one")
    public void monitorWebForOne(Model model, String id) {
        //这是选中后的电池单体id
        model.addAttribute("id", id);
        // 1. 实体类
        EquipmentManage equipmentManage = equipmentManageService.findOne(id);
        model.addAttribute("equipmentManage", equipmentManage);
        if (StringUtils.isBlank(equipmentManage.getCode())) {
            model.addAttribute("message", "请在工程配置->对象管理->设备管理页面设置设备唯一标识，否则无法显示");
            return;
        }
        // 3. 实时监控
        List<ParameterInfo> parameterInfoList = monitorService.findListByTypeForMonitor(equipmentManage.getEquipmentType(), equipmentManage);
        model.addAttribute("parameterInfoList", parameterInfoList);

        /**
         * 电池单体诊断分析数据请求
         */
        Map<String, String> dtsocData = diagnoseResultInfoService.getDtData(id);
        model.addAttribute("dtsocData", dtsocData);
    }

    /**
     * 电池单体历史曲线
     * @param id
     * @param objType
     * @param name
     * @param ranges
     * @param freque
     * @param show_type
     * @param start_time
     * @param end_time
     * @return
     */
    @RequestMapping("history_battery_equipment")
    @ResponseBody
    public Map historyDataEquipment(String id, int objType, String name, String ranges,
                                    String freque, String show_type, String start_time, String end_time) {
        if ("custom".equals(ranges) && StringUtils.isBlank(start_time) && StringUtils.isBlank(end_time)){
            return null;
        }
        return monitorService.historyDataEquipment(id, objType, name, ranges, freque, show_type, start_time, end_time);
    }
    /**
     * 电池单体SOC诊断请求
     */
    @RequestMapping("dtsoc_data_equipment")
    @ResponseBody
    public JSONObject getMonitorDtsoc(String id, Map<String, String> newDataDC, String dtSoc) {

        Map<String, String> dtsocNewData = diagnoseResultInfoService.getDtsocData(id, newDataDC, dtSoc);
        JSONObject data = JSONObject.parseObject(JSONObject.toJSONString(dtsocNewData));
        System.out.println(data);
        return data;
    }

    /**
     * 电池单体容量诊断请求
     */
    @RequestMapping("dtrl_data_equipment")
    @ResponseBody
    public JSONObject getMonitorDtrl(String id, Map<String, String> newDataDC, String dtSoc) {

        Map<String, String> dtsocNewData = diagnoseResultInfoService.getDtrlData(id, newDataDC, dtSoc);
        JSONObject data = JSONObject.parseObject(JSONObject.toJSONString(dtsocNewData));
        System.out.println(data);
        return data;
    }

    /**
     * 电池单体容量诊断请求
     */
    @RequestMapping("dtzz_data_equipment")
    @ResponseBody
    public JSONObject getMonitorDtzz(String id, Map<String, String> newDataDC, String dtSoc) {

        Map<String, String> dtsocNewData = diagnoseResultInfoService.getDtzzData(id, newDataDC, dtSoc);
        JSONObject data = JSONObject.parseObject(JSONObject.toJSONString(dtsocNewData));
        System.out.println(data);
        return data;
    }

}
